package com.vivo.tasks.vivofx;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.employee.entity.ViEmployee;
import com.vivo.employee.service.ViEmployeeService;
import com.vivo.fx.employee.service.FxEmployeeinfoService;
import com.vivo.fx.sys.entity.FxUserinfo;
import com.vivo.fx.sys.service.FxUserinfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TaskViUserinfo {

    private Logger logger = Logger.getLogger(TaskViEmployee.class);
    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private FxUserinfoService fxUserinfoService;

    @Autowired
    private SystemService systemService;

    @Autowired
    private ViEmployeeService viEmployeeService;

    private Map<String, String> roleMap = new HashMap<String, String>();

    private Map<String, Role> strRoleMap = new HashMap<String, Role>();

    public void SyncData() {
        roleMap.put("V顾问", "53f5a1d908544c5daf9bd5897c02ec16");
        roleMap.put("V专家", "03c895eefae14abb995ba5063ec3b0e4");
        roleMap.put("促销员", "7a348754defd49eea30a294cb894ded3");
        roleMap.put("V督导", "02d16f9e2cc0452590f63a033042c8a6");
        roleMap.put("业务员", "5d9f237d942f4fc48d99c09830b522aa");
        roleMap.put("库管", "1d637f31c6ab45b98329f5c13d574886");
        roleMap.put("销售支持", "ae669625a2694e1abc77d07b594c787d");
        roleMap.put("经理", "3a93cb42348f4de985a43eedec9e1b74");

        for (String roleName: roleMap.keySet()) {
            Role role = systemService.getRole(roleMap.get(roleName));
            if (role != null) {
                strRoleMap.put(roleName, role);
            }
        }

        System.out.println("role list size is :" + strRoleMap.size());

        getDataInfo("ah");
        getDataInfo("js");
    }


    private void getDataInfo(String area) {
        System.out.println("get user info from " + area + " ...");

        List<FxUserinfo> fxUserinfoList = null;
        try {
            // 切换数据库
            Helper.changeDB(area);

//            fxEmployeeinfos = fxEmployeeinfoService.findList(new FxEmployeeinfo());

            int pageStart = 1;

            Page<FxUserinfo> page = new Page<FxUserinfo>(pageStart, 2000);

            Page<FxUserinfo> pageRes = fxUserinfoService.findPage(page, new FxUserinfo());
            System.out.println("cur page is :" + pageRes.getPageNo() + " and pageSize is :" + pageRes.getTotalPage());

            DatabaseContextHolder.setMainDb();
            saveUserInfo(pageRes.getList(), area);

            for (int i = pageStart + 1; i < pageRes.getTotalPage()+1; i++) {
                System.out.println("cur page is :" + pageRes.getPageNo() + " and pageSize is :" + pageRes.getTotalPage());
                page.setPageNo(i);

                Helper.changeDB(area);
                pageRes = fxUserinfoService.findPage(page, new FxUserinfo());

                DatabaseContextHolder.setMainDb();
                saveUserInfo(pageRes.getList(), area);
            }

        } finally {
            DatabaseContextHolder.setMainDb();
        }

        System.out.println("get employee info from " + area + " end !");
    }

    private void saveUserInfo(List<FxUserinfo> userinfos, String area) {
        for(FxUserinfo fu: userinfos) {

            ViEmployee ve = viEmployeeService.get(fu.getEmployeeid());
            User user = systemService.getUser(fu.getUserid());
            if (ve != null) {
                if (user == null) {
                    user = new User();
                    user.setId(fu.getUserid());
                } else {
                    continue;
                }

                user.setIsTaskInsert(true);

                user.setEmployeeId(fu.getEmployeeid());
//                user.setPassword("02a3f0772fcca9f415adc990734b45c6f059c7d33ee28362c4852032");

                // 没有手机号码使用姓名登陆
                if (ve.getTelephone() == null || ve.getTelephone().length() < 11) {
                    user.setPassword("02a3f0772fcca9f415adc990734b45c6f059c7d33ee28362c4852032");
                    user.setLoginName(fu.getUsername());
                } else {
                    user.setPassword(SystemService.entryptPassword(ve.getTelephone().substring(5, 11)));
                    user.setLoginName(ve.getTelephone());
                }

                user.setOffice(ve.getDept());
                user.setCompany(ve.getComp());
                user.setPosition(ve.getPost());
                user.setMobile(ve.getTelephone());

                user.setName(ve.getName());
                user.setNo(ve.getCode());
                user.setZone(area);

                systemService.saveUser(user);

                // 取消设置插入选项
                user.setIsTaskInsert(false);
                Role role = strRoleMap.get(ve.getPost().getName());
                if (role != null) {
                    systemService.assignUserToRole(role, user);
                }

            }
        }
    }
}
